package com.licw.controller.admin;

import com.licw.dto.CategoryDTO;
import com.licw.dto.CategoryPageQueryDTO;
import com.licw.entity.Category;
import com.licw.result.PageResult;
import com.licw.result.Result;
import com.licw.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation("新增分类")
    public Result<Object> add(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult<Category>> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查看分类：{}", categoryPageQueryDTO);
        PageResult<Category> page = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        log.info("根据类型查询分类：{}",type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result<Object> deleteById(Long id){
        log.info("根据id查询分类：{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改分类信息
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<Object> edit(@RequestBody CategoryDTO categoryDTO){
        log.info("更新分类：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 禁用/解禁分类
     * @param status 状态值
     * @param id 分类id
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result<Object> setStatus(@PathVariable Integer status, Long id){
        log.info("禁用/解禁分类：{}", id);
        categoryService.setStatus(status, id);
        return Result.success();
    }

}
